/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.shop.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.base.DeleteReq;
import me.zhengjie.modules.shop.domain.ProductCart;
import me.zhengjie.modules.shop.domain.resp.MyCartListResp;
import me.zhengjie.modules.shop.service.ProductCartService;
import me.zhengjie.modules.shop.domain.vo.ProductCartQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author yiwe.he
* @date 2024-04-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "购物车管理")
@RequestMapping("/api/productCart")
public class ProductCartController {

    private final ProductCartService productCartService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportProductCart(HttpServletResponse response, ProductCartQueryCriteria criteria) throws IOException {
        productCartService.download(productCartService.queryAll(criteria), response);
    }

    @PostMapping("/list")
    @Log("我的购物车")
    @ApiOperation("我的购物车")
    public ResponseEntity<PageResult<MyCartListResp>> queryProductCart(@RequestBody ProductCartQueryCriteria criteria){
        return new ResponseEntity<>(productCartService.queryAll(criteria,criteria.getPage()),HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log("添加购物车")
    @ApiOperation("添加购物车")
    public ResponseEntity<Boolean> add(@Validated @RequestBody ProductCart resources) throws BizException {
        return new ResponseEntity<>(productCartService.add(resources),HttpStatus.OK);
    }

    @PutMapping
    @Log("修改购物车")
    @ApiOperation("修改购物车")
    public ResponseEntity<Object> updateProductCart(@Validated @RequestBody ProductCart resources){
        productCartService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除购物车")
    @ApiOperation("删除购物车")
    public ResponseEntity<Boolean> deleteProductCart(@RequestBody DeleteReq req) {
        return new ResponseEntity<>(productCartService.deleteAll(req.getIds()),HttpStatus.OK);
    }

    @PostMapping("/callBack")
    @Log("支付回调")
    @ApiOperation("支付回调")
    public ResponseEntity<Boolean> callBack(@RequestBody ProductCartQueryCriteria criteria) throws BizException {
        return new ResponseEntity<>(productCartService.callBack(criteria),HttpStatus.OK);
    }


}